extends ../layout

block meta
    link(rel="stylesheet", href="#{options.cdn_path}/js/artDialog/skins/default.css?v=#{options.site_version}")
    style
        ul, li {
            list-style-type: none;
        }
        span.tag {
            display: inline-block;
            background: #FFF;
            border: 1px solid #e6e6e6;
            padding: 0 8px;
            height: 24px;
            line-height: 24px;
            max-width: 160px;
            overflow: hidden;
            color: #666;
        }
        span.tag:hover {
            border-color: #BBB;
            color: #999;
            text-decoration: none;
        }
        a.icon-remove {
            vertical-align: top;
            margin-left: 4px;
        }
        .attached {
            display: inline;
        }
        #addtags {
            border-radius: 3px;
            margin-left: 5px;
            margin-bottom: 8px;
        }
    //include ../common/_kindeditor

block content
    // 传递参数
    dashboardClass = true
    tab = blog.id ? '修改博文' : '写博'
    include ../common/_admin_navbar
    .content
        .header
            h1.page-title #{tab}
        ul.breadcrumb
            li
                a(href="/admin") 首页
                span.divider /
            li.active #{tab}
        .container-fluid
            .row-fluid
                form(action="/admin/blog/save_update", method="post")
                    input(type="hidden", name="blog.id", value="#{blog.id}")
                    .btn-toolbar
                        button.btn.btn-primary(type="submit")
                            i.icon-save 
                            | &nbsp;保存
                        a.btn(href="/admin/blog/list") 返回
                        .btn-group
                    .well
                        #myTabContent.tab-content
                            label 标题
                            input.input-xxlarge(type="text", name="blog.title", value="#{blog.title}", required="required")
                            label 分类
                            select#DropDownTimezone.input-xlarge(name="blog.blog_type")
                                types = [{'id': 0, 'type_name': '原创'}, {'id': 1, 'type_name': '收藏'}]
                                each item in types
                                    if item.id == blog.blog_type
                                        option(value="#{item.id}", selected="selected") #{item.type_name}
                                    else
                                        option(value="#{item.id}") #{item.type_name}
                            label 标签
                            ul.tag-list
                                if tags != null
                                    each item in tags
                                        li.attached
                                            span.tag #{item.tag_name}
                                            a.icon-remove(title="删除", href="#")
                                            input(name="tags", value="#{item.id}", type="hidden")
                            input#autocomplete.input-xlarge(type="text")
                            a#addtags.btn.btn-primary
                                i.icon-tag
                                | 添加标签
                            label 来源
                            input.input-xxlarge(type="text", name="blog.share_url", value="#{blog.share_url}")
                            label 内容
                            textarea#editor.input-xlarge(name="blog.content") !{blog.content}

                include ../common/_admin_footer

block script
    script(src="#{options.cdn_path}/js/artDialog/jquery.artDialog.min.js?v=#{options.site_version}")
    script(src="#{options.cdn_path}/js/autocomplete/bootstrap.autocomplete.js?v=#{options.site_version}")
    // ueditor
    script(type="text/javascript", charset="utf-8", src="/ueditor/ueditor.config.js")
    script(type="text/javascript", charset="utf-8", src="#{options.cdn_path}/ueditor/ueditor.all.js")
    script
        $(function(){
            // ueditor
            var editor = UE.getEditor('editor');
            // 标签处理
            $('#autocomplete').autocomplete({
                source:function(query, callback){
                    var count = this.options.items;
                    $.get("/admin/tags/all", {"query" : query, "count" : count}, function(data){
                        return callback(data);
                    });
                },
                formatItem:function(item){
                    return item.TAG_NAME;
                },
                setValue:function(item){
                    return {'data-value': item.TAG_NAME, 'data-id': item.ID};
                }
            });
            // 删除标签
            $(document).on("click", ".icon-remove", function(e){
                $(this).parents('.attached').remove();
                return false;
            })
            // form 提交
            $('form').submit(function(){
                editor.sync()
                var dialog = $.dialog();
                var title = $('input[name="blog.title"]').val();
                var content = $('#editor').val();
                if( title.length < 3 ){
                    dialog.content('标题太短！').time(1000);
                    return false;
                }
                if( content.length < 10 ){
                    dialog.content('亲再多些点吧！').time(1000);
                    return false;
                }
                _post(this, dialog, function(data){
                    if(data.status){
                        dialog.content('保存成功！').lock().time(1000);
                        setTimeout(function(){
                            location.href = '/admin/blog/list';
                        }, 1500);
                    }else{
                        dialog.content('服务器忙，请稍候！').lock().time(2000);
                    }
                });
                return false;
            });
            $('#addtags').click(function(){
                var tagname = $('#autocomplete').val();
                $.post('/admin/tags/add_tags',{tagName: tagname}, function(data){
                    if (data.status == 0) {
                        $(".tag-list").append('<li class="attached"><span class="tag">' + tagname + '</span><a class="icon-remove" title="删除" href="#"></a><input name="tags" value="' + data.tagsid + '" type="hidden"></li>')
                    } else {
                        alert("该标签已经存在，不能重复添加！");
                    }
                });
            });
        });